//
//  YZXTabBarController.swift
//  YZXWeibo
//
//  Created by 叶周翔 on 16/7/18.
//  Copyright © 2016年 叶周翔. All rights reserved.
//

import UIKit

class YZXTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //使用KVC将tabbar转换成自定义的
        let tab = YZXTabBar()
        tab.callBack = {() -> Void in
            // 弹出一个控制器
            // 弹出的控制器必须是要被一个导航控制器嵌套着的 
            if YZXUserAccountVM.shareUserAccountVM.userLogin{
                
                self.presentViewController(UINavigationController(rootViewController: YZXComposeController()), animated: true, completion: nil)
                
            }else{
                
                self.presentViewController(UINavigationController(rootViewController: YZXLoginController()), animated: true, completion: nil)
            }
        }
        
        self.setValue(tab, forKey: "tabBar")
        
        // 调用方法
        self.setUpControllers()
    }
    
    func setUpControllers(){
        // 首页控制器
        let homeVC = YZXHomeController()
        // 设置tabBar控制器
        self.addViewControllers(homeVC, title: "首页", imageName: "tabbar_home")
        // 消息控制器
        let messageVC = YZXMessageController()
        self.addViewControllers(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        // 发现
        let discover = YZXDiscoverController()
        self.addViewControllers(discover, title: "发现", imageName: "tabbar_discover")
        
        // 我
        let profileVC = YZXProfileController()
        self.addViewControllers(profileVC, title: "我", imageName: "tabbar_profile")
        
    
        
    }
    
    
    /// 抽取一个方法
    func addViewControllers(controller:UIViewController, title:String, imageName:String){
        
        controller.title = title
        // 设置图片
        controller.tabBarItem.image = UIImage(named: imageName)
        // 选中的图片
        let selectName = imageName+"_selected"
        // 设置标题的属性
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        // 设置选中的图片
        controller.tabBarItem.selectedImage = UIImage(named: selectName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        // 导航控制器添加根控制器
        
        self.addChildViewController(UINavigationController(rootViewController: controller))
        
        
    }
    
    
    
}
